@namespace RazorConsole.Components

@using System.Collections.Generic
@using System.Globalization
@using Microsoft.AspNetCore.Components
@using RazorConsole.Core.Rendering.ComponentMarkup
@using Spectre.Console

<div class="panel"
     data-header="@TitleAttribute"
     data-header-color="@HeaderColorAttribute"
     data-border-color="@BorderColorAttribute"
     data-expand="@ExpandAttribute"
     data-border="@BorderAttribute"
     data-padding="@PaddingAttribute"
     data-height="@HeightAttribute"
     data-width="@WidthAttribute">
    @ChildContent
</div>

@code {
    [Parameter]
    public RenderFragment? ChildContent { get; set; }

    private static readonly IReadOnlyDictionary<BoxBorder, string> BorderNames = new Dictionary<BoxBorder, string>
{
        { BoxBorder.Square, "square" },
        { BoxBorder.Rounded, "rounded" },
        { BoxBorder.Double, "double" },
        { BoxBorder.Heavy, "heavy" },
        { BoxBorder.Ascii, "ascii" },
        { BoxBorder.None, "none" },
    };

    [Parameter]
    public string? Title { get; set; }

    [Parameter]
    public Color? TitleColor { get; set; }

    [Parameter]
    public Color? BorderColor { get; set; }

    [Parameter]
    public BoxBorder? Border { get; set; }

    [Parameter]
    public int? Height { get; set; }

    [Parameter]
    public Padding? Padding { get; set; }

    [Parameter]
    public int? Width { get; set; }

    [Parameter]
    public bool Expand { get; set; }

    private string? TitleAttribute => string.IsNullOrWhiteSpace(Title) ? null : Title;

    private string? HeaderColorAttribute => TitleColor?.ToString();

    private string? BorderColorAttribute => BorderColor?.ToString();

    private string? BorderAttribute
    {
        get
        {
            if (Border is null)
            {
                return null;
            }

            if (BorderNames.TryGetValue(Border, out var name))
            {
                return name;
            }

            return Border.GetType().Name.ToLowerInvariant();
        }
    }

    private string ExpandAttribute => Expand ? "true" : "false";

    private string? PaddingAttribute
    {
        get
        {
            if (Padding is not { } padding)
            {
                return null;
            }

            return string.Join(",",
                padding.Left.ToString(CultureInfo.InvariantCulture),
                padding.Top.ToString(CultureInfo.InvariantCulture),
                padding.Right.ToString(CultureInfo.InvariantCulture),
                padding.Bottom.ToString(CultureInfo.InvariantCulture));
        }
    }

    private string? HeightAttribute => Height.HasValue && Height.Value > 0
        ? Height.Value.ToString(CultureInfo.InvariantCulture)
        : null;

    private string? WidthAttribute => Width.HasValue && Width.Value > 0
        ? Width.Value.ToString(CultureInfo.InvariantCulture)
        : null;
}
